| Conditions | 17 |
| Paths | 8 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like getRenderProperties.js ➔ getRenderProperties often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* global HTMLImageElement */ |
||
| 21 | function getRenderProperties(element){ |
||
| 22 | // If the element is a string, query select call again |
||
| 23 | if(typeof element === "string"){ |
||
| 24 | return querySelectedRenderProperties(element); |
||
| 25 | } |
||
| 26 | // If element is array. Recursivly call with every object in the array |
||
| 27 | else if(Array.isArray(element)){ |
||
| 28 | var returnArray = []; |
||
| 29 | for(let i = 0; i < element.length; i++){ |
||
| 30 | returnArray.push(getRenderProperties(element[i])); |
||
| 31 | } |
||
| 32 | return returnArray; |
||
| 33 | } |
||
| 34 | // If element, render on canvas and set the uri as src |
||
| 35 | else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLImageElement){ |
||
| 36 | return newCanvasRenderProperties(element); |
||
| 37 | } |
||
| 38 | // If SVG |
||
| 39 | else if( |
||
| 40 | (element && element.nodeName === 'svg') || |
||
| 41 | (typeof SVGElement !== 'undefined' && element instanceof SVGElement) |
||
| 42 | ){ |
||
| 43 | return { |
||
| 44 | element: element, |
||
| 45 | options: getOptionsFromElement(element), |
||
| 46 | renderer: renderers.SVGRenderer |
||
| 47 | }; |
||
| 48 | } |
||
| 49 | // If canvas (in browser) |
||
| 50 | else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLCanvasElement){ |
||
| 51 | return { |
||
| 52 | element: element, |
||
| 53 | options: getOptionsFromElement(element), |
||
| 54 | renderer: renderers.CanvasRenderer |
||
| 55 | }; |
||
| 56 | } |
||
| 57 | // If canvas (in node) |
||
| 58 | else if(element && element.getContext){ |
||
| 59 | return { |
||
| 60 | element: element, |
||
| 61 | renderer: renderers.CanvasRenderer |
||
| 62 | }; |
||
| 63 | } |
||
| 64 | else if(element && typeof element === 'object' && !element.nodeName) { |
||
| 65 | return { |
||
| 66 | element: element, |
||
| 67 | renderer: renderers.ObjectRenderer |
||
| 68 | }; |
||
| 69 | } |
||
| 70 | else{ |
||
| 71 | throw new InvalidElementException(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 103 |